package ;
import haxe.Http;
//import me.cunity.php.dom.DOMDocument;
//import me.cunity.php.dom.DOMElement;
import php.db.Connection;
import php.io.File;
import php.Session;
import php.Web;
import UserType;
/**
 * ...
 * @author Axel Huizinga axel@cunity.me
 */


class Auth 
{
	var cnx:Connection;
	var param:Hash<String>;
	public var res:StringBuf;
	public var userType:UserType;
	
	public function new(cx:Connection) 
	{
		cnx = cx;
		param = Main.param;
		res = new StringBuf();
	}
	
	public function login():Bool
	{
		Main.errLog('');
		var u:User = User.manager.object("SELECT * FROM User WHERE name LIKE '" +param.get('name') + "'", true);
		if (u != null)
		{	
			if (u.password == param.get('open'))
			{
				u.ip = Web.getClientIP();
				u.sid = Session.getId();
				u.update();
				Main.userType = Type.createEnum(UserType, u.type);
				Session.set('IP', u.ip);
				Session.set('AUTH', 'logout');
				return true;
			}
			res.add('<div>Passwort nicht korrekt :-(</div>');
			return false;
		}
		res.add('<div>Benutzer:');
		res.add(param.get('name') + ' unbekannt :-(</div>');
		return false;
	}
	
	public function logout()
	{
		var u:User =  User.manager.object(
			"SELECT * FROM User WHERE ip LIKE '" +Web.getClientIP() + "' AND sid LIKE '" + Session.getId() + "'"
		, true);
		Session.set('AUTH', 'login');
		if (u == null)
		{
			res.add('<div function="back"/>');
			Session.remove('IP');
			Main.errLog('IP removed:' + Session.get('IP'));
			return false;
		}
		u.ip = '';
		u.sid = '';
		u.update();
		Session.remove('IP');
		//Main.param.set('path', Session.get('PATH_PREV'));
		Main.param.set('path', 'dologout');
		Main.errLog('reset path 2 :' + Session.get('PATH_PREV'));
		Main.run();
		//res.add('<function name = "sLink" path="home"/>');
		return true;
	}
	
	public function checkAuth():Bool
	{
		var u:User =  User.manager.object(
			"SELECT * FROM User WHERE ip LIKE '" +Web.getClientIP() + "' AND sid LIKE '" + Session.getId() + "'"
		,false);
		if (u == null)
		{
			res.add('guest');
			Main.userType = UserType.guest;
			return false;
		}
		Main.userType = Type.createEnum(UserType, u.type);
		res.add(u.type);
		return true;
	}
	
	public function admin():Bool
	{
		var u:User =  User.manager.object(
			"SELECT * FROM User WHERE ip LIKE '" +Web.getClientIP() + "' AND sid LIKE '" + Session.getId() + "'"
		,false);
		Main.errLog('user:' + u);
		if (u == null || getUserType(u) != UserType.admin)
		{
			res.add('<div class="error"><a href="menu.php?path=login" target="menuFrame">Bitte neu Anmelden!</a>');
			res.add('</div>');
			return false;
		}

		switch(param.get('task'))
		{
			case 'delete':

			var cat:Cats = Cats.manager.get(Std.parseInt(param.get('id')));
			if (cat != null)
			{
				cat.delete();
				Main.output(Cats.displayAll(UserType.admin));
			}
			else
			{
				Main.output(Cats.displayAll(UserType.admin));
				Main.errLog('cat:' + param.get('id') + ' not found!');
				return false;
			}

			case 'editCat':
			Main.output(Cats.edit(Std.parseInt(param.get('id'))));
			
			case 'changeCat':
			Main.output(Cats.manager.get(Std.parseInt(param.get('id'))).change(param), false);
			//Cats.manager.get(Std.parseInt(param.get('id'))).change(param);
			//Main.output(Cats.displayAll(UserType.admin), false);
			//res.add(Cats.change(param));
			case 'new':
			new Cats(param);
			//Main.errLog(Cats.displayAll(UserType.admin));
			//Main.output('<body>' + Cats.displayAll(UserType.admin) + '</body>', false);
			Main.output(Cats.displayAll(UserType.admin), false);
			
			case 'changeLink':
			Main.output(Link.manager.get(Std.parseInt(param.get('id'))).change(param), false);
			//Main.output(Link.displayAll(UserType.admin));
			
			case 'editLink':
			Main.output(Link.edit(Std.parseInt(param.get('id'))));
			
			case 'deleteLink':
			var link:Link = Link.manager.get(Std.parseInt(param.get('id')));
			if (link != null)
			{
				link.delete();
				Main.output( Link.displayAll(UserType.admin), false);
			}
			else
			{
				Main.errLog('Link:' + param.get('id') + ' nicht gefunden!');
				Main.output( Link.displayAll(UserType.admin), false);
				return false;
			}
			case 'newLink':
			new Link(param);
			//Main.errLog(Cats.displayAll(UserType.admin));
			Main.output( Link.displayAll(UserType.admin), false);
			//trace(param.toString());
			default:
			return false;
		}
		return true;
	}
	
	public function getUserType(?aU:User):UserType
	{
		var u:User = aU == null ? User.manager.object(
			"SELECT * FROM User WHERE ip LIKE '" +Web.getClientIP() + "' AND sid LIKE '" + Session.getId() + "'", true
		) : aU;
		//Main.errLog(u +' IP:' +Web.getClientIP()+ ' sid:' +Session.getId());
		if (u == null)
			return UserType.guest;
		return (u.type == 'admin' ? UserType.admin : UserType.user);
	}
	
}